#!/bin/bash

# this is executed in the docker fpm image
set -e
cd /build

# the init scripts needs to have the right name
cp ${RUNID}.init /tmp/{{.beat_name}}.init

# create script to reload systemd config
echo "#!/bin/bash" > /tmp/systemd-daemon-reload.sh
echo "systemctl daemon-reload 2> /dev/null || true" >> /tmp/systemd-daemon-reload.sh

# add SNAPSHOT if it was requested
VERSION="{{.version}}"
if [ "$SNAPSHOT" = "yes" ]; then
    VERSION="${VERSION}-SNAPSHOT"
fi

# create deb
fpm --force -s dir -t deb \
        -n {{.beat_name}} -v ${VERSION} \
        --vendor "Elastic" \
        --license "ASL 2.0" \
        --architecture {{.deb_arch}} \
        --description "{{.beat_description}}" \
        --url {{.beat_url}} \
        --deb-init /tmp/{{.beat_name}}.init \
        --after-install /tmp/systemd-daemon-reload.sh \
        --config-files /etc/{{.beat_name}}/{{.beat_name}}.yml \
        homedir/=/usr/share/{{.beat_name}} \
        beatname-${RUNID}.sh=/usr/bin/{{.beat_name}}.sh \
        {{.beat_name}}-linux-{{.arch}}=/usr/share/{{.beat_name}}/bin/{{.beat_name}} \
        {{.beat_name}}-linux.yml=/etc/{{.beat_name}}/{{.beat_name}}.yml \
        {{.beat_name}}-linux.full.yml=/etc/{{.beat_name}}/{{.beat_name}}.full.yml \
        {{.beat_name}}.template.json=/etc/{{.beat_name}}/{{.beat_name}}.template.json \
        {{.beat_name}}.template-es2x.json=/etc/{{.beat_name}}/{{.beat_name}}.template-es2x.json \
        ${RUNID}.service=/lib/systemd/system/{{.beat_name}}.service \
        god-linux-{{.arch}}=/usr/share/{{.beat_name}}/bin/{{.beat_name}}-god \
        import_dashboards-linux-{{.arch}}=/usr/share/{{.beat_name}}/scripts/import_dashboards

# move and rename to use the elastic conventions
mkdir -p upload
mv {{.beat_name}}_${VERSION}_{{.deb_arch}}.deb upload/{{.beat_name}}-${VERSION}-{{.deb_arch}}.deb
echo "Created upload/{{.beat_name}}-${VERSION}-{{.deb_arch}}.deb"

# create sha1 file
cd upload
sha1sum {{.beat_name}}-${VERSION}-{{.deb_arch}}.deb | awk '{print $1;}' > {{.beat_name}}-${VERSION}-{{.deb_arch}}.deb.sha1
echo "Created upload/{{.beat_name}}-${VERSION}-{{.deb_arch}}.deb.sha1"
